﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

namespace leodesigner
{
    public partial class SplashScreen : Form
    {
        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        System.Timers.Timer timer;
        float t;
        System.Drawing.Bitmap b;

        public SplashScreen()
        {
            InitializeComponent();
			b=new Bitmap("kopter.png");
			SetBitmap(b,0);
            timer = new System.Timers.Timer(100);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(everythingIsOver);
            timer.Enabled = true;
            t = 0;
/*            Opacity = 0.9;*/
        }
  private void everythingIsOver(object source, System.Timers.ElapsedEventArgs e)
        {
            timer.Enabled = false;
            t += 1 / 10.0f;

            if (t > 2)
            {
                b.Dispose();
                this.Close();
                return;
            }

            float a=t/2;
            a *= 2; a -= 1;
            a = 1 - a*a;
//            a=a*a*((3.0f)-(2.0f*a));
            byte d = (byte)(a * 255);
            SetBitmap(b, d);
            timer.Enabled = true;
  /*          this.Opacity=0.9;*/
        }


public void SetBitmap(Bitmap bitmap) 
		{
			SetBitmap(bitmap, 255);
		}

protected override CreateParams CreateParams	
		{
			get
			{
				CreateParams cp = base.CreateParams;
				cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style
				return cp;
			}
		}

	public void SetBitmap(Bitmap bitmap, byte opacity) 
		{
            IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
            IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
            IntPtr hBitmap = IntPtr.Zero;
            IntPtr oldBitmap = IntPtr.Zero;

			try 
			{
				hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));  // grab a GDI handle from this GDI+ bitmap
				oldBitmap = Win32.SelectObject(memDc, hBitmap);

                int horzSize = Win32.GetDeviceCaps(screenDc, 8);
                int vertSize = Win32.GetDeviceCaps(screenDc, 10);
				Win32.Size size=new Win32.Size(bitmap.Width, bitmap.Height);
                Win32.Point pointSource = new Win32.Point(0,0);
				Win32.Point topPos=new Win32.Point((horzSize-bitmap.Width)/2,(vertSize-bitmap.Height)/2);//{horzSize/2-size.cx/2,vertSize/2-size.cy/2};
				Win32.BLENDFUNCTION blend;
				blend.BlendOp             = Win32.AC_SRC_OVER;
				blend.BlendFlags          = 0;
				blend.SourceConstantAlpha = opacity;
                blend.AlphaFormat = Win32.AC_SRC_ALPHA;

//				UpdateLayeredWindow((HWND)Handle.ToPointer(), screenDc, &topPos, &size, memDc, &pointSource, 0, &blend, ULW_ALPHA);
				Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
			}
			finally 
			{
                Win32.ReleaseDC(IntPtr.Zero, screenDc);
				if (hBitmap != IntPtr.Zero) 
				{
                    Win32.SelectObject(memDc, oldBitmap);
					//Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak.
                    Win32.DeleteObject(hBitmap);
				}
                Win32.DeleteDC(memDc);
			}
		}
    }
}
